#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2016 lizongzhe 
#
# Distributed under terms of the MIT license.

import re
import logging
import urllib
import requests
import base64
from bs4 import BeautifulSoup
from proxy_manager.core import ProxyHost
from proxy_manager.resource.base import BaseProxyResource

logger = logging.getLogger(__name__)

COUNTRIES = (
        ("any","所有"),
        ("CN","中国"),
        ("DK","丹麦"),
        ("UA","乌克兰"),
        ("UY","乌拉圭"),
        ("AM","亚美尼亚"),
        ("IL","以色列"),
        ("IQ","伊拉克"),
        ("IR","伊朗"),
        ("RU","俄罗斯"),
        ("BG","保加利亚"),
        ("CA","加拿大"),
        ("HU","匈牙利"),
        ("ZA","南非"),
        ("RW","卢旺达"),
        ("IN","印度"),
        ("ID","印度尼西亚"),
        ("EC","厄瓜多尔"),
        ("TW","台湾"),
        ("KG","吉尔吉斯斯坦"),
        ("KZ","哈萨克斯坦"),
        ("CO","哥伦比亚"),
        ("TR","土耳其"),
        ("EG","埃及"),
        ("RS","塞尔维亚"),
        ("SL","塞拉利昂"),
        ("MX","墨西哥"),
        ("DO","多米尼加共和国"),
        ("AT","奥地利"),
        ("VE","委内瑞拉"),
        ("BD","孟加拉国"),
        ("NG","尼日利亚"),
        ("NP","尼泊尔"),
        ("PK","巴基斯坦"),
        ("PY","巴拉圭"),
        ("PA","巴拿马"),
        ("BR","巴西"),
        ("GR","希腊"),
        ("DE","德国"),
        ("IT","意大利"),
        ("LV","拉脱维亚"),
        ("CZ","捷克"),
        ("MD","摩尔多瓦"),
        ("SK","斯洛伐克"),
        ("SG","新加坡"),
        ("JP","日本"),
        ("CL","智利"),
        ("KH","柬埔寨"),
        ("GE","格鲁吉亚"),
        ("EU","欧洲"),
        ("BE","比利时"),
        ("FR","法国"),
        ("PL","波兰"),
        ("TH","泰国"),
        ("ZW","津巴布韦"),
        ("AU","澳大利亚"),
        ("MO","澳门"),
        ("TT","特里尼达和多巴哥"),
        ("BO","玻利维亚"),
        ("SE","瑞典"),
        ("CH","瑞士"),
        ("PE","秘鲁"),
        ("RO","罗马尼亚"),
        ("US","美国"),
        ("KE","肯尼亚"),
        ("GB","英国"),
        ("NL","荷兰"),
        ("PH","菲律宾"),
        ("ES","西班牙"),
        ("ZM","赞比亚"),
        ("VN","越南"),
        ("AZ","阿塞拜疆"),
        ("AL","阿尔巴尼亚"),
        ("AR","阿根廷"),
        ("AE","阿联酋"),
        ("KR","韩国"),
        ("HK","香港"),
        ("MV","马尔代夫"),
        ("MY","马来西亚"),
        ("LB","黎巴嫩"),
)


SCHEMES = (
    ('https', 'https'),
    ('http', 'http'),
)

AVAILABILITY = [(value, ">={}%".format(value)) for value in range(0, 100, 10)]

class ProxyResource(BaseProxyResource):
    url = 'http://proxy-list.org/english/search.php'
    def __init__(self, country='any', scheme='http', pages=2):
        self.country = country
        self.scheme = scheme
        self.pages = pages

    def download(self):
        params = {}
        if self.scheme == 'https':
            if self.country == 'any':
                search = 'ssl-yes'
            else:
                search = '{}.ssl-yes'.format(self.country)
            ssl = 'yes'
        else:
            search = 'ssl-no'
            ssl = 'no'

        params['search'] = search
        params['country'] = self.country
        params['ssl'] = ssl

        resps = []
        for page in range(self.pages):
            params['p'] = page + 1
            params_query = urllib.urlencode(params, True)
            url = self.url + params_query
            resps.append(requests.get(url))
        return resps

    def parse(self, pages):
        result = []
        for page in pages:
            soup = BeautifulSoup(page.text, 'html.parser')
            for proxy_elem in soup.select('li[class=proxy]'):
                try:
                    proxies = re.findall("Proxy\('(.*)'\)", proxy_elem.text)
                    for proxy_loc in proxies:
                        proxy_loc = base64.b64decode(proxy_loc).decode('utf-8')
                        ip, port = proxy_loc.split(':')
                        proxy_host = ProxyHost(ip=ip, port=port, scheme=self.scheme)
                        result.append(proxy_host)
                except Exception as e:
                    logger.error(e, exc_info=True)
        return result

    def get_proxies(self):
        try:
            pages = self.download()
            data = self.parse(pages)
            return data
        except Exception as e:
            logger.error(e, exc_info=True)
            raise e

